package com.ruoyi.framework.config;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.dao.SaTokenDaoForRedisson;
import com.ruoyi.common.utils.RedisUtil;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.serialization.jackson.JacksonRenderFactory;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.solon.RedissonClientOriginalSupplier;

/**
 * Redis配置
 * @author ruoyi
 */
@Configuration
public class RedisConfig {

    /**
     * RedissonClient
     * @param supplier
     * @return
     */
    @Bean
    @Condition(onBean = JacksonConfig.class)
    public RedissonClient redissonClient(@Inject("${redis.cache_source}") RedissonClientOriginalSupplier supplier, @Inject JacksonRenderFactory factory) {
        // 创建 RedissonClient之前设置序列化方式（跟项目序列化方式一致）
        supplier.withConfig(config -> config.setCodec(new JsonJacksonCodec(factory.config())));
        return supplier.get();
    }

    /**
     * SaTokenDao
     * @param redissonClient
     * @return
     */
    @Bean
    @Condition(onBean = RedissonClient.class)
    public SaTokenDao saTokenDaoInit(RedissonClient redissonClient) {
        return new SaTokenDaoForRedisson(redissonClient);
    }

    /**
     * RedisUtil
     * @return
     */
    @Bean
    @Condition(onBean = RedissonClient.class)
    public RedisUtil redisUtil(RedissonClient redissonClient) {
        return new RedisUtil(redissonClient);
    }

}
